package com.zbkj.business.sys.config;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import com.tiangong.framework.util.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.BaseService;

import java.util.Arrays;
import java.util.List;

/**
 * @description: tbconfig配置信息表
 * Author: jason
 * @create: ${creatTime}
 **/
@Service("config")
@Slf4j
@OpenApi
@DS("prod")
public class ConfigService extends BaseService<ConfigDao, Config> implements IService<Config> {

    /**
     * list
     */
    public Return list(CDO cdoRequest) {
        cdoRequest.clearBlankValue();
        return super.getPageList(cdoRequest);
    }

    /**
     * info
     */
    public Return info(CDO cdoRequest) {
        return super.get(cdoRequest);
    }

    /**
     * save
     */
    public Return save(CDO cdoRequest) {
        cdoRequest.clearBlankValue();
        super.save(cdoRequest);
        cache();
        return Return.SUCCESS;
    }

    /**
     * update
     */
    public Return update(CDO cdoRequest) {
         super.updateById(cdoRequest);
        cache();
        return Return.SUCCESS;
    }

    /**
     * delete
     */
    public Return delete(CDO cdoRequest) {
        String[] ids = cdoRequest.getStringValue("ids").split(",");
        boolean b = super.removeBatchByIds(Arrays.asList(ids));
        if (b) {
            return Return.SUCCESS;
        }
        return Return.EXCEPTION;
    }

    @Scheduled(cron = "0 0/1 * * * ?")
    public void cache() {
        List<Config> configs = this.baseMapper.selectList(new LambdaQueryWrapper<Config>());
        configs.forEach(config -> {
            RedisUtil.getInstance().set("CONFIG:" + config.getId(), config.getContent());
        });
    }
}
